var row =
	{
		init : function()
			{
		        this.status = new Ext.form.TextArea
		            ({
						height: 200,
						readOnly: true,
						autoscroll: false,
						listeners:
							{
								'change': function()
									{
										if(this.autoscroll == true)
										{
											this.el.scrollTo('top', this.el.dom.scrollHeight);
										}
										else
										{
											this.el.scrollTo('top', this.el.lastScrollTop);
										}
									}
							},
						preappend: function(value)
							{
								this.el.lastScrollTop = this.el.getScroll().top;
								if(!Ext.isEmpty(this.getValue()))
								{
									this.setValue(this.getValue() + "\r\n" + value);
								}
								else
								{
									this.setValue(value);
								}
								this.fireEvent('change');
							}
		            });

				this.autoscroll = new Ext.Panel
		            ({
						layout: 'form',
						items: new Ext.form.Checkbox
							({
								fieldLabel: 'Auto-scroll',
								listeners:
									{
										'check': function(checkbox, checked)
											{
												row.status.autoscroll = checked;
											},
										'render': function()
											{
												this.setValue(true);
											}
									}
							})
		            });

				var sincronizar = function(button, event, url)
					{
						var hasUrl = (typeof url != 'undefined');
						var msg = Ext.MessageBox.wait('Processando', 'Aguarde');

						if(!hasUrl) row.status.preappend((this.msgprefix ? this.msgprefix : '')+"Procurando Tags relacionadas a newsletters e em uso ...");
						this.msgprefix = "\r\n";

						Ext.Ajax.request
							({
								url: (hasUrl) ? url : _PHP_.form.url.submit,
								success: function(response)
									{
										msg.hide();
										var response = Ext.util.JSON.decode(response.responseText);

										if(response.success === 'true')
										{
											row.status.preappend(response.message);

											if(typeof response.tags != 'undefined')
											{
												var tags = Ext.util.JSON.decode(response.tags);
												for(var i=0;i<tags.length;i++)
												{
													var tag = tags[i];

													if(typeof response.url != 'undefined')
													{
														row.status.preappend('Processando a tag ('+(i+1)+' de '+tags.length+'): '+tag['nome']);
														sincronizar(null, null, response.url+ '/' +tag['id']+'/'+tag['cliente_id']+'/'+tag['lista_id']);
													}
												}
											}
										}
										else
										{
											msg.alert('Erro', response.erro);
										}
									}
							});
					};

				var form = application.items.get(0);
				form.on('beforerender', function(_self)
					{
						this.buttons[0].text = 'Sincronizar';
						this.buttons[0].handler = sincronizar;
						this.buttons = [this.buttons[0]];
						this.layout = 'fit';
						
						this.keys =
							{
							    key: Ext.EventObject.ENTER,
							    fn: function()
									{
										_self.buttons[0].handler.apply(_self);		
									}
							};
						
						this.add(row.status, row.autoscroll);
					});
					
				application.show();
			}
	};